#include <stdio.h>
#include <string.h>
long htoi(char []);
int main()
{
    int k;
    char num[1024];
    scanf("%d", &k);
    getchar();
    while(k--){
        gets(num);
        printf("%ld\n", htoi(num));
    }
    return 0;
}
long htoi(char num[]){
    int len = strlen(num), n, i, j;
    long sum = 0, temp;
    for(i = 0; i < len; i ++){
        if(num[i] >= 'a' && num[i] <= 'z')
            n = num[i] - 'a' + 10;
        else if(num[i] >= 'A' && num[i] <= 'Z')
            n = num[i] - 'A' + 10;
        else
            n = num[i]-'0';
        j = len - i - 1;
        temp = 16;
        if(j == 0)
            temp = 1;
        else
            while(--j>0)
                temp *= 16;
        sum += temp * n;
    }
    return sum;
}
